import { Injectable } from '@nestjs/common';
import {
  DistributionApply_Create_Dto,
  DistributionApply_Pass_Dto,
} from './assets/distributionApply.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { DistributionApply } from './assets/distributionApply.entity';
import { EntityManager, Repository } from 'typeorm';
import { DistributionApply_Find_Dto } from './assets/distributionApply.dto';
import { util_Db_PageList } from '@/common/util/db';
import { IRequest } from '@/common/typeScripts';
import { UserClient } from '../userClient/assets/userClient.entity';
import { InviteRelation } from '../inviteRelation/assets/inviteRelation.entity';
import { Constant_Enum_Enum_UserClient_Type } from '@/common/constants/enum';
import { Class_HttpException_Base } from '@/common/classes/httpException';

@Injectable()
export class DistributionApply_Service {
  constructor(
    @InjectRepository(DistributionApply)
    private distributionApply_Repository: Repository<DistributionApply>,
    @InjectRepository(InviteRelation)
    private inviteRelation_Repository: Repository<InviteRelation>,
    @InjectRepository(UserClient)
    private userClient_Repository: Repository<UserClient>,
    private entityManager: EntityManager,
  ) {}

  async distributionApply_FindByPage(query: DistributionApply_Find_Dto) {
    return await util_Db_PageList<DistributionApply>({
      queryBuilder: this.distributionApply_Repository
        .createQueryBuilder('distributionApply')
        .leftJoin('distributionApply.userClient', 'userClient')
        .select([
          'distributionApply.id AS id',
          'distributionApply.daCreateTime AS daCreateTime',
          'distributionApply.daStatus AS daStatus',
          'distributionApply.daLinkWay AS daLinkWay',
          'distributionApply.daRemark AS daRemark',
          'distributionApply.daProcessTime AS daProcessTime',
          'userClient.id AS userClientId',
          'userClient.userName AS userName',
        ]),
      query: {
        'userClient.id': query.userClientId,
        'userClient.userClientName': query.userClientName,
        daStatus: query.daStatus,
      },
      isRaw: true,
    });
  }

  async distributionApply_FindOneById(id: number) {
    return await this.distributionApply_Repository
      .createQueryBuilder('distributionApply')
      .andWhere(`distributionApply.id = :id`, { id: id })
      .getOne();
  }

  async distributionApply_Pass(body: DistributionApply_Pass_Dto) {
    return await this.entityManager.transaction(async (manage) => {
      const distributionApply = await this.distributionApply_Repository
        .createQueryBuilder('distributionApply')
        .leftJoinAndSelect('distributionApply.userClient', 'userClient')
        .andWhere(`distributionApply.id = :id`, { id: body.id })
        .getOne();
      if (!distributionApply) throw new Class_HttpException_Base('不存在该记录');
      console.log(distributionApply);

      const inviteRelation = await this.userClient_Repository
        .createQueryBuilder('userClient')
        .where('userClient.id = :id', { id: distributionApply.userClient.id })
        .getOne();
      console.log(inviteRelation);
      if (!distributionApply) throw new Class_HttpException_Base('不存在该用户');
      await manage.update(UserClient, inviteRelation.id, {
        distributionType: Constant_Enum_Enum_UserClient_Type.UserClient_Type_Lock,
      });
      await manage.update(DistributionApply, distributionApply.id, { daStatus: body.daStatus });
      return '审核成功';
    });
  }

  async distributionApply_Web_Submit(body: DistributionApply_Create_Dto, req: IRequest) {
    await this.distributionApply_Repository.save({
      daLinkWay: body.daLinkWay,
      daRemark: body.daRemark,
      userClient: this.userClient_Repository.create({ id: req.user.userId }),
    });
    return '提交成功';
  }
}
